<?php 
/*
** etc:  控制器
** author: deepe.xiao
** update: 2018-10-10
*/

namespace Core;

class Controller{

	private $tp = null;
	var $get;        //$_GET 
	var $post;       //$_POST

	function __construct(){
		$this->tp = loadLibrary("Mytp");
		spl_autoload_register("self::models_autoload");
	}


	function assign($name,$value){
		$this->tp->assign($name,$value);
	}
	function diplay($tplName){
		$this->tp->tp($tplName);
	}

	/**
	 * 当在控制器中需要用到模型类时,自动调用
	 * 注意自动加载类依靠命名空间规范,请注意与文件夹名称一致
	 * @param $classname
	 */
	private function models_autoload($classname){

		$classname = str_replace('\\', '/', $classname);

		if( strlen( $classname ) != 0 ) {
			$fdir = FCPATH .$classname. ".php";
			if (!file_exists($fdir)) {
				view_log('Model Not Exists', 'The model not exists <font color="red">' .$fdir . '</font>');
			}
			include_once( $fdir );

		}else{
			view_log('Model null error','Model not allow have null');
		}
	}


	/**
	 * 加载模型层
	 * @param $model 模型名字
	 * @param $flag 是否实例化数据库
	 *
	 */
	function loadModel( $model,  $flag=false , $version_dir = ""  ) {
		$this->Mrecodes = "";
		
		if( $version_dir == "" ){
			$version_dir = config_item( 'sp_version' );
			$version_dir = config_item('pj_name')."_".$version_dir[0];
			
		}

		if( strlen( $model ) != 0 ){
			if ( !file_exists( FCPATH."/app/models/".$version_dir."/".$model.".php" ) ){
				view_log('Model Not Exists','The model not exists <font color="red">'.FCPATH."app/models/".$model.".php".'</font>');
			}
			
			include_once( FCPATH."app/models/".$version_dir."/".$model.".php" );

			$model = "M".$model;
			$this->$model = new $model();
			$this->$model->post = $this->post;
			$this->$model->get = $this->get;
			if( $flag ){
				$this->$model->loadMysql();
			}
		}else{
			view_log('Model null error','Model not allow have null');
		}
		return $this->$model;
	}
	
	//加载管理员模型
	function loadAdminModel( $model,  $flag=false  ){
		$this->Mrecodes = "";
		if( strlen( $model ) != 0 ){
			if ( !file_exists( FCPATH."/app/models/admin/".$model.".php" ) ){
				view_log('Model Not Exists','The model not exists <font color="red">'.FCPATH."app/models/".$model.".php".'</font>');
			}
			include_once( FCPATH."app/models/admin/".$model.".php" );
			$model = "M".$model;
			$this->$model = new $model();
			$this->$model->post = $this->post;
			$this->$model->get = $this->get;
			if( $flag ){
				$this->$model->loadMysql();
			}
		}else{
			view_log('Model null error','Model not allow have null');
		}
		return $this->$model;
	}

	//加载视图层
	function loadView( $view, $param='' ){
		if( strlen( $view ) != 0 ){
			if ( !file_exists( FCPATH."app/views/".$view.".html" ) ){
				view_log('View Not Exists','The model not exists <font color="red">'.FCPATH."app/views/".$view.".html".'</font>');
			}
			$data = $param;             //Ò³ÃæÉÏµÄÊý×é±äÁ¿ÃûÎª data
			include_once( FCPATH."app/views/".$view.".html" );
		}else{
			view_log('View null error','View not allow have null');
		}
	}

	//检测用户是否存在
    function checkUser(){
        $route = new Route();
        $login = $route->autoLoadClass("login");
        $login->checkUser();
    }

    //屏蔽用户
    function only_script(){
        if( PHP_SAPI !== 'cli' ){
            header('http/1.1 403 Forbidden');
            die();
        }
    }

	/**
	 * 进入到成功的页面
	 * @param String $message 成功的消息内容
	 * @param String $jumpUrl 成功后跳转的地址
	 * @param int $waitSecond (可选)等待的时间(秒)
	 */
    function success($message,$jumpUrl,$waitSecond = 1){
		$this->jump($message,$jumpUrl,$waitSecond,true);
	}

	/**
	 * 进入到失败的页面
	 * @param String $message 失败的消息内容
	 * @param String $jumpUrl (可选)失败后跳转的地址 例:index.php?aciton=user&func=index
	 * @param int $waitSecond (可选)等待的时间(秒)
	 */
	function error($message,$jumpUrl="javascript:history.go(-1);",$waitSecond = 3){
		$this->jump($message,$jumpUrl,$waitSecond,false);
	}
	private function jump($message,$jumpUrl="javascript:history.go(-1);",$waitSecond = 3,$isSuccess){
		$tp = loadLibrary("Mytp");
		if($isSuccess){
			$tp->assign("message",$message);
		}else{
			$tp->assign("error",$message);
		}
		$tp->assign("jumpUrl",$jumpUrl);
		$tp->assign("waitSecond",$waitSecond);
		$tp->tp("public/jump");
	}

}
?>
